package com.aaa.day1106.day110601;


import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ${崔灿}
 * @date 2022/11/06 12:58
 **/

public class Test01 {
    public static void main(String[] args) {
        List<Person1> personList = new ArrayList<>();
        personList.add(new Person1("欧阳雪",18,"中国",'F'));
        personList.add(new Person1("Tom",24,"美国",'M'));
        personList.add(new Person1("Harley",22,"英国",'F'));
        personList.add(new Person1("向天笑",20,"中国",'M'));
        personList.add(new Person1("李康",22,"中国",'M'));
        personList.add(new Person1("小梅",20,"中国",'F'));
        personList.add(new Person1("何雪",21,"中国",'F'));

        //筛选出年龄大于20
        //personList.stream().filter(item-> item.getAge()>20).forEach(item-> System.out.println(item));

        //personList.stream().filter(item-> item.getAge()>20).forEach(System.out::println);

        //map映射操作--名称
        //personList.stream().map(item->item.getName()).forEach(System.out::println);

        //排序操作
        //personList.stream().sorted((o1,o2)->Integer.compare(o1.getAge(),o2.getAge())).forEach(System.out::println);

        //find操作--findFirst获取流中第一个元素
        //Optional<Person1> first = personList.stream().findFirst();
        //System.out.println(first.get());

        //find操作---findAny() 获取流中任意一个元素 必须使用并行流测试
        //Optional<Person1> any = personList.parallelStream().findAny();
        //System.out.println(any.get());

        //匹配操作--所有元素都必须匹配条件才返回true
        //boolean b = personList.stream().allMatch(item->item.getAge()>20);
        //System.out.println(b);
        //boolean a = personList.stream().anyMatch(item->item.getAge()>10);
        //System.out.println(a);

        //聚合操作---count
        //long count = personList.stream().count();
        //System.out.println("个数："+count);

        //聚合操作---max , min
        //Optional<Person1> max = personList.stream().max((o1,o2)->o1.getAge()-o2.getAge());
        //System.out.println(max);
//聚合操作---max , min
        //Optional<Person1> min = personList.stream().min((o1,o2)->o1.getAge() - o2.getAge());
        //System.out.println(min);

        //收集个数
        //Long collect=personList.stream().collect(Collectors.counting());
        //System.out.println(collect);

        //计算平均年龄
        //Double collect = personList.stream().collect(Collectors.averagingInt(item->item.getAge()));
        //System.out.println(collect);

        //最大年龄
        //Optional<Person1> collect = personList.stream().collect(Collectors.maxBy((o1,o2)->o1.getAge()-o2.getAge()));
        //System.out.println(collect);

        //求年龄的和
        //Integer collect = personList.stream().collect(Collectors.summingInt(item->item.getAge()));
        //System.out.println(collect);

        //收集为一个新的集合
        //Set<String> set = personList.stream().filter(item->item.getAge()>20).filter(item->item.getCountry().equals("中国")).map(item->item.getName()).collect(Collectors.toSet());
        //System.out.println(set);
        //Map<String,Object> map = personList.stream().filter(item->item.getAge()>20).filter(item->item.getCountry().equals("中国")).collect(Collectors.toMap(item->item.getName(),item->item));
        //System.out.println(map);

        //分组操作
        Map<Object,List<Person1>> collect = personList.stream().collect(Collectors.groupingBy(item->item.getSex()));
        System.out.println(collect);

    }
}
class Person1 {
    private String name;
    private Integer age;
    private String country;
    private char sex;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", country='" + country + '\'' +
                ", sex=" + sex +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public Person1(String name, Integer age, String country, char sex) {
        this.name = name;
        this.age = age;
        this.country = country;
        this.sex = sex;
    }
}
